{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    <div class=\"bk-root\">\n",
       "        <a href=\"https://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
       "        <span id=\"94f4564e-c349-4ff9-a346-8da040940694\">Loading BokehJS ...</span>\n",
       "    </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "(function(root) {\n",
       "  function now() {\n",
       "    return new Date();\n",
       "  }\n",
       "\n",
       "  var force = true;\n",
       "\n",
       "  if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
       "    root._bokeh_onload_callbacks = [];\n",
       "    root._bokeh_is_loading = undefined;\n",
       "  }\n",
       "\n",
       "  var JS_MIME_TYPE = 'application/javascript';\n",
       "  var HTML_MIME_TYPE = 'text/html';\n",
       "  var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n",
       "  var CLASS_NAME = 'output_bokeh rendered_html';\n",
       "\n",
       "  /**\n",
       "   * Render data to the DOM node\n",
       "   */\n",
       "  function render(props, node) {\n",
       "    var script = document.createElement(\"script\");\n",
       "    node.appendChild(script);\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when an output is cleared or removed\n",
       "   */\n",
       "  function handleClearOutput(event, handle) {\n",
       "    var cell = handle.cell;\n",
       "\n",
       "    var id = cell.output_area._bokeh_element_id;\n",
       "    var server_id = cell.output_area._bokeh_server_id;\n",
       "    // Clean up Bokeh references\n",
       "    if (id != null && id in Bokeh.index) {\n",
       "      Bokeh.index[id].model.document.clear();\n",
       "      delete Bokeh.index[id];\n",
       "    }\n",
       "\n",
       "    if (server_id !== undefined) {\n",
       "      // Clean up Bokeh references\n",
       "      var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n",
       "      cell.notebook.kernel.execute(cmd, {\n",
       "        iopub: {\n",
       "          output: function(msg) {\n",
       "            var id = msg.content.text.trim();\n",
       "            if (id in Bokeh.index) {\n",
       "              Bokeh.index[id].model.document.clear();\n",
       "              delete Bokeh.index[id];\n",
       "            }\n",
       "          }\n",
       "        }\n",
       "      });\n",
       "      // Destroy server and session\n",
       "      var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n",
       "      cell.notebook.kernel.execute(cmd);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when a new output is added\n",
       "   */\n",
       "  function handleAddOutput(event, handle) {\n",
       "    var output_area = handle.output_area;\n",
       "    var output = handle.output;\n",
       "\n",
       "    // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n",
       "    if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n",
       "      return\n",
       "    }\n",
       "\n",
       "    var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n",
       "\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n",
       "      toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n",
       "      // store reference to embed id on output_area\n",
       "      output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n",
       "    }\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n",
       "      var bk_div = document.createElement(\"div\");\n",
       "      bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n",
       "      var script_attrs = bk_div.children[0].attributes;\n",
       "      for (var i = 0; i < script_attrs.length; i++) {\n",
       "        toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n",
       "      }\n",
       "      // store reference to server id on output_area\n",
       "      output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n",
       "    }\n",
       "  }\n",
       "\n",
       "  function register_renderer(events, OutputArea) {\n",
       "\n",
       "    function append_mime(data, metadata, element) {\n",
       "      // create a DOM node to render to\n",
       "      var toinsert = this.create_output_subarea(\n",
       "        metadata,\n",
       "        CLASS_NAME,\n",
       "        EXEC_MIME_TYPE\n",
       "      );\n",
       "      this.keyboard_manager.register_events(toinsert);\n",
       "      // Render to node\n",
       "      var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n",
       "      render(props, toinsert[toinsert.length - 1]);\n",
       "      element.append(toinsert);\n",
       "      return toinsert\n",
       "    }\n",
       "\n",
       "    /* Handle when an output is cleared or removed */\n",
       "    events.on('clear_output.CodeCell', handleClearOutput);\n",
       "    events.on('delete.Cell', handleClearOutput);\n",
       "\n",
       "    /* Handle when a new output is added */\n",
       "    events.on('output_added.OutputArea', handleAddOutput);\n",
       "\n",
       "    /**\n",
       "     * Register the mime type and append_mime function with output_area\n",
       "     */\n",
       "    OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n",
       "      /* Is output safe? */\n",
       "      safe: true,\n",
       "      /* Index of renderer in `output_area.display_order` */\n",
       "      index: 0\n",
       "    });\n",
       "  }\n",
       "\n",
       "  // register the mime type if in Jupyter Notebook environment and previously unregistered\n",
       "  if (root.Jupyter !== undefined) {\n",
       "    var events = require('base/js/events');\n",
       "    var OutputArea = require('notebook/js/outputarea').OutputArea;\n",
       "\n",
       "    if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n",
       "      register_renderer(events, OutputArea);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  \n",
       "  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
       "    root._bokeh_timeout = Date.now() + 5000;\n",
       "    root._bokeh_failed_load = false;\n",
       "  }\n",
       "\n",
       "  var NB_LOAD_WARNING = {'data': {'text/html':\n",
       "     \"<div style='background-color: #fdd'>\\n\"+\n",
       "     \"<p>\\n\"+\n",
       "     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
       "     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
       "     \"</p>\\n\"+\n",
       "     \"<ul>\\n\"+\n",
       "     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
       "     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
       "     \"</ul>\\n\"+\n",
       "     \"<code>\\n\"+\n",
       "     \"from bokeh.resources import INLINE\\n\"+\n",
       "     \"output_notebook(resources=INLINE)\\n\"+\n",
       "     \"</code>\\n\"+\n",
       "     \"</div>\"}};\n",
       "\n",
       "  function display_loaded() {\n",
       "    var el = document.getElementById(\"94f4564e-c349-4ff9-a346-8da040940694\");\n",
       "    if (el != null) {\n",
       "      el.textContent = \"BokehJS is loading...\";\n",
       "    }\n",
       "    if (root.Bokeh !== undefined) {\n",
       "      if (el != null) {\n",
       "        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n",
       "      }\n",
       "    } else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(display_loaded, 100)\n",
       "    }\n",
       "  }\n",
       "\n",
       "\n",
       "  function run_callbacks() {\n",
       "    try {\n",
       "      root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
       "    }\n",
       "    finally {\n",
       "      delete root._bokeh_onload_callbacks\n",
       "    }\n",
       "    console.info(\"Bokeh: all callbacks have finished\");\n",
       "  }\n",
       "\n",
       "  function load_libs(js_urls, callback) {\n",
       "    root._bokeh_onload_callbacks.push(callback);\n",
       "    if (root._bokeh_is_loading > 0) {\n",
       "      console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
       "      return null;\n",
       "    }\n",
       "    if (js_urls == null || js_urls.length === 0) {\n",
       "      run_callbacks();\n",
       "      return null;\n",
       "    }\n",
       "    console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
       "    root._bokeh_is_loading = js_urls.length;\n",
       "    for (var i = 0; i < js_urls.length; i++) {\n",
       "      var url = js_urls[i];\n",
       "      var s = document.createElement('script');\n",
       "      s.src = url;\n",
       "      s.async = false;\n",
       "      s.onreadystatechange = s.onload = function() {\n",
       "        root._bokeh_is_loading--;\n",
       "        if (root._bokeh_is_loading === 0) {\n",
       "          console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
       "          run_callbacks()\n",
       "        }\n",
       "      };\n",
       "      s.onerror = function() {\n",
       "        console.warn(\"failed to load library \" + url);\n",
       "      };\n",
       "      console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
       "      document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
       "    }\n",
       "  };var element = document.getElementById(\"94f4564e-c349-4ff9-a346-8da040940694\");\n",
       "  if (element == null) {\n",
       "    console.log(\"Bokeh: ERROR: autoload.js configured with elementid '94f4564e-c349-4ff9-a346-8da040940694' but no matching script tag was found. \")\n",
       "    return false;\n",
       "  }\n",
       "\n",
       "  var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.13.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.13.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.13.0.min.js\"];\n",
       "\n",
       "  var inline_js = [\n",
       "    function(Bokeh) {\n",
       "      Bokeh.set_log_level(\"info\");\n",
       "    },\n",
       "    \n",
       "    function(Bokeh) {\n",
       "      \n",
       "    },\n",
       "    function(Bokeh) {\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.min.css\");\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.13.0.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.13.0.min.css\");\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.13.0.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.13.0.min.css\");\n",
       "    }\n",
       "  ];\n",
       "\n",
       "  function run_inline_js() {\n",
       "    \n",
       "    if ((root.Bokeh !== undefined) || (force === true)) {\n",
       "      for (var i = 0; i < inline_js.length; i++) {\n",
       "        inline_js[i].call(root, root.Bokeh);\n",
       "      }if (force === true) {\n",
       "        display_loaded();\n",
       "      }} else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(run_inline_js, 100);\n",
       "    } else if (!root._bokeh_failed_load) {\n",
       "      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
       "      root._bokeh_failed_load = true;\n",
       "    } else if (force !== true) {\n",
       "      var cell = $(document.getElementById(\"94f4564e-c349-4ff9-a346-8da040940694\")).parents('.cell').data().cell;\n",
       "      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
       "    }\n",
       "\n",
       "  }\n",
       "\n",
       "  if (root._bokeh_is_loading === 0) {\n",
       "    console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
       "    run_inline_js();\n",
       "  } else {\n",
       "    load_libs(js_urls, function() {\n",
       "      console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
       "      run_inline_js();\n",
       "    });\n",
       "  }\n",
       "}(window));"
      ],
      "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n  function now() {\n    return new Date();\n  }\n\n  var force = true;\n\n  if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n    root._bokeh_onload_callbacks = [];\n    root._bokeh_is_loading = undefined;\n  }\n\n  \n\n  \n  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n    root._bokeh_timeout = Date.now() + 5000;\n    root._bokeh_failed_load = false;\n  }\n\n  var NB_LOAD_WARNING = {'data': {'text/html':\n     \"<div style='background-color: #fdd'>\\n\"+\n     \"<p>\\n\"+\n     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n     \"</p>\\n\"+\n     \"<ul>\\n\"+\n     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n     \"</ul>\\n\"+\n     \"<code>\\n\"+\n     \"from bokeh.resources import INLINE\\n\"+\n     \"output_notebook(resources=INLINE)\\n\"+\n     \"</code>\\n\"+\n     \"</div>\"}};\n\n  function display_loaded() {\n    var el = document.getElementById(\"94f4564e-c349-4ff9-a346-8da040940694\");\n    if (el != null) {\n      el.textContent = \"BokehJS is loading...\";\n    }\n    if (root.Bokeh !== undefined) {\n      if (el != null) {\n        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n      }\n    } else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(display_loaded, 100)\n    }\n  }\n\n\n  function run_callbacks() {\n    try {\n      root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n    }\n    finally {\n      delete root._bokeh_onload_callbacks\n    }\n    console.info(\"Bokeh: all callbacks have finished\");\n  }\n\n  function load_libs(js_urls, callback) {\n    root._bokeh_onload_callbacks.push(callback);\n    if (root._bokeh_is_loading > 0) {\n      console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n      return null;\n    }\n    if (js_urls == null || js_urls.length === 0) {\n      run_callbacks();\n      return null;\n    }\n    console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n    root._bokeh_is_loading = js_urls.length;\n    for (var i = 0; i < js_urls.length; i++) {\n      var url = js_urls[i];\n      var s = document.createElement('script');\n      s.src = url;\n      s.async = false;\n      s.onreadystatechange = s.onload = function() {\n        root._bokeh_is_loading--;\n        if (root._bokeh_is_loading === 0) {\n          console.log(\"Bokeh: all BokehJS libraries loaded\");\n          run_callbacks()\n        }\n      };\n      s.onerror = function() {\n        console.warn(\"failed to load library \" + url);\n      };\n      console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n      document.getElementsByTagName(\"head\")[0].appendChild(s);\n    }\n  };var element = document.getElementById(\"94f4564e-c349-4ff9-a346-8da040940694\");\n  if (element == null) {\n    console.log(\"Bokeh: ERROR: autoload.js configured with elementid '94f4564e-c349-4ff9-a346-8da040940694' but no matching script tag was found. \")\n    return false;\n  }\n\n  var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.13.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.13.0.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.13.0.min.js\"];\n\n  var inline_js = [\n    function(Bokeh) {\n      Bokeh.set_log_level(\"info\");\n    },\n    \n    function(Bokeh) {\n      \n    },\n    function(Bokeh) {\n      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.min.css\");\n      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.min.css\");\n      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.13.0.min.css\");\n      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.13.0.min.css\");\n      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.13.0.min.css\");\n      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.13.0.min.css\");\n    }\n  ];\n\n  function run_inline_js() {\n    \n    if ((root.Bokeh !== undefined) || (force === true)) {\n      for (var i = 0; i < inline_js.length; i++) {\n        inline_js[i].call(root, root.Bokeh);\n      }if (force === true) {\n        display_loaded();\n      }} else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(run_inline_js, 100);\n    } else if (!root._bokeh_failed_load) {\n      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n      root._bokeh_failed_load = true;\n    } else if (force !== true) {\n      var cell = $(document.getElementById(\"94f4564e-c349-4ff9-a346-8da040940694\")).parents('.cell').data().cell;\n      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n    }\n\n  }\n\n  if (root._bokeh_is_loading === 0) {\n    console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n    run_inline_js();\n  } else {\n    load_libs(js_urls, function() {\n      console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n      run_inline_js();\n    });\n  }\n}(window));"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import norm\n",
    "import matplotlib.pylab as plt\n",
    "import pandas as pd\n",
    "from bokeh.layouts import row, widgetbox, layout, gridplot\n",
    "from bokeh.models import CustomJS, Slider\n",
    "from bokeh.plotting import figure, output_file, show, ColumnDataSource\n",
    "from bokeh.models.glyphs import MultiLine\n",
    "from bokeh.io import output_notebook\n",
    "from bokeh.models.widgets import Div\n",
    "%matplotlib inline\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "num_data = 5\n",
    "X = norm.rvs(size=(num_data,2), random_state=42)\n",
    "X[:,0]=X[:,0]+5\n",
    "X = np.dot(X, np.linalg.cholesky([[1, 0.6], [0.6, 0.6]]))\n",
    "m = X.mean(axis=0)\n",
    "X = X - m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.28551678, -0.30882627],\n",
       "       [ 0.8622341 ,  0.50503833],\n",
       "       [-1.0739079 , -0.35579408],\n",
       "       [ 1.3404013 ,  0.13487383],\n",
       "       [-0.84321071,  0.02470818]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x11de64710>"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAD8CAYAAACfF6SlAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAADtxJREFUeJzt3V2MXGd9x/HvD8eBlXgxJAZiJ8FB\nWK7cUuF2FdFWfVEb5KQXtkuhTdSKRAqyEIp60cpSrFSoChe8WBVVRVTVpaiBCwJErnFbV1sIUG4a\nmo1MMSFaYqLS2BuRpeBIFQs45t8Lj63NMvs6Z3d29/l+pNXOnHk0z3M89nfHZ2bOpqqQJLXlJcNe\ngCRp9Rl/SWqQ8ZekBhl/SWqQ8ZekBhl/SWqQ8ZekBhl/SWqQ8ZekBl017AXM5dprr60dO3YMexmS\ntK48/vjj36uqrQuNW7Px37FjB+Pj48NehiStK0m+s5hxHvaRpAYZf0lqkPGXpAYZf0lqkPGXpAYZ\nf0lqkPGXpAYZf0lq0Jr9kJekth0/dY4jYxNMnp9m25YRDu3dxYE924e9rA3D+Etac46fOsfhY6eZ\nvnARgHPnpzl87DSAPwA64mEfSWvOkbGJK+G/bPrCRY6MTQxpRRuP8Ze05kyen17Sdi2d8Ze05mzb\nMrKk7Vo64y9pzTm0dxcjmze9aNvI5k0c2rtrSCvaeHzBV9Kac/lFXd/ts3KMv6Q16cCe7cZ+BXnY\nR5IaZPwlqUHGX5IaZPwlqUHGX5IaZPwlqUHGX5IaZPwlqUHGX5IaZPwlqUHGX5IaZPwlqUHGX5Ia\nZPwlqUHGX5Ia1En8k9yaZCLJmST3zjPuHUkqyWgX80qSlmfg+CfZBDwA3AbsBu5IsrvPuFcAfwJ8\nddA5JUmD6eKZ/83Amap6uqp+AjwE7O8z7v3Ah4EfdTCnJGkAXcR/O/DMjOtne9uuSLIHuKGq/rmD\n+SRJA+oi/umzra7cmLwE+AjwZwveUXIwyXiS8ampqQ6WJknqp4v4nwVumHH9emByxvVXAL8AfDnJ\nfwNvBU70e9G3qo5W1WhVjW7durWDpUmS+uki/o8BO5PclORq4HbgxOUbq+r5qrq2qnZU1Q7gUWBf\nVY13MLckaRkGjn9VvQDcA4wBTwKfqaonktyfZN+g9y9J6t5VXdxJVZ0ETs7a9r45xv5WF3NKkpbP\nT/hKUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhL\nUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOM\nvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoOMvyQ1qJP4J7k1yUSS\nM0nu7XP7nyb5ZpKvJ3kkyRu6mFeStDwDxz/JJuAB4DZgN3BHkt2zhp0CRqvqF4GHgQ8POq8kafm6\neOZ/M3Cmqp6uqp8ADwH7Zw6oqi9V1Q97Vx8Fru9gXknSMnUR/+3AMzOun+1tm8vdwL/2uyHJwSTj\nScanpqY6WJokqZ8u4p8+26rvwOSPgVHgSL/bq+poVY1W1ejWrVs7WJokqZ+rOriPs8ANM65fD0zO\nHpTkFuA+4Der6scdzCtJWqYunvk/BuxMclOSq4HbgRMzByTZA/wtsK+qnutgTknSAAaOf1W9ANwD\njAFPAp+pqieS3J9kX2/YEeDlwGeTfC3JiTnuTpK0Cro47ENVnQROztr2vhmXb+liHklSN/yEryQ1\nyPhLUoOMvyQ1yPhLUoOMvyQ1yPhLUoM6eaunJGkwx0+d48jYBJPnp9m2ZYRDe3dxYM98p0kbjPGX\npCE7fuoch4+dZvrCRQDOnZ/m8LHTACv2A8DDPpI0ZEfGJq6E/7LpCxc5MjaxYnMaf0kassnz00va\n3gXjL0lDtm3LyJK2d8H4S9KQHdq7i5HNm160bWTzJg7t3bVic/qCryQN2eUXdX23jyQ15sCe7Ssa\n+9k87CNJDTL+ktQg4y9JDTL+ktQg4y9JDTL+ktQg4y9JDTL+ktQg4y9JDTL+ktQg4y9JDTL+ktQg\n4y9JDTL+ktQg4y9JDTL+ktQgf5lLR46fOreqv4VHkgZh/Dtw/NQ5Dh87zfSFiwCcOz/N4WOnAfwB\nIGlN8rBPB46MTVwJ/2XTFy5yZGxiSCuSpPkZ/w5Mnp9e0nZJGjbj34FtW0aWtF2Shq2T+Ce5NclE\nkjNJ7u1z+0uTfLp3+1eT7Ohi3rXi0N5djGze9KJtI5s3cWjvriGtSJLmN3D8k2wCHgBuA3YDdyTZ\nPWvY3cAPqupNwEeADw0671pyYM92PvD2N7N9ywgBtm8Z4QNvf7Mv9kpas7p4t8/NwJmqehogyUPA\nfuCbM8bsB/6id/lh4KNJUlXVwfxrwoE92429pHWji8M+24FnZlw/29vWd0xVvQA8D1zTwdySpGXo\nIv7ps232M/rFjCHJwSTjScanpqY6WJokqZ8u4n8WuGHG9euBybnGJLkKeBXw/dl3VFVHq2q0qka3\nbt3awdIkSf10Ef/HgJ1JbkpyNXA7cGLWmBPAnb3L7wC+uJGO90vSejPwC75V9UKSe4AxYBPw8ap6\nIsn9wHhVnQD+HvhkkjNcesZ/+6DzSpKWr5Nz+1TVSeDkrG3vm3H5R8A7u5hLkjQ4P+ErSQ0y/pLU\nIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMv\nSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y\n/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUIOMvSQ0y/pLUoIHin+Q1ST6f5Kne91f3GfOW\nJP+R5IkkX0/yh4PMKUka3KDP/O8FHqmqncAjveuz/RB4V1X9PHAr8FdJtgw4ryRpAIPGfz/wYO/y\ng8CB2QOq6ltV9VTv8iTwHLB1wHklSQMYNP6vq6pnAXrfXzvf4CQ3A1cD357j9oNJxpOMT01NDbg0\nSdJcrlpoQJIvAK/vc9N9S5koyXXAJ4E7q+qn/cZU1VHgKMDo6Ggt5f4lSYu3YPyr6pa5bkvy3STX\nVdWzvbg/N8e4VwL/Avx5VT267NVKkjox6GGfE8Cdvct3Ap+bPSDJ1cA/Ap+oqs8OOJ8kqQODxv+D\nwNuSPAW8rXedJKNJPtYb8wfAbwB3Jfla7+stA84rSRpAqtbmofXR0dEaHx8f9jIkaV1J8nhVjS40\nzk/4SlKDjL8kNcj4S1KDjL8kNcj4S1KDjL8kNcj4S1KDjL8kNcj4S1KDjL8kNcj4S1KDjL8kNcj4\nS1KDjL8kNcj4S1KDFvw1jtJyHT91jiNjE0yen2bblhEO7d3FgT3bh70sSRh/rZDjp85x+Nhppi9c\nBODc+WkOHzsN4A8AaQ3wsI9WxJGxiSvhv2z6wkWOjE0MaUWSZjL+WhGT56eXtF3S6jL+WhHbtows\nabuk1WX8tSIO7d3FyOZNL9o2snkTh/buGtKKJM3kC75aEZdf1PXdPtLaZPy1Yg7s2W7spTXKwz6S\n1CDjL0kNMv6S1KANeczf0wpI0vw2XPw9rYAkLWzDHfbxtAKStLANF39PKyBJC9tw8fe0ApK0sA0X\nf08rIEkL23Av+HpaAUla2IaLP3haAUlayIY77CNJWthA8U/ymiSfT/JU7/ur5xn7yiTnknx0kDkl\nSYMb9Jn/vcAjVbUTeKR3fS7vB/59wPkkSR0YNP77gQd7lx8EDvQblOSXgdcB/zbgfJKkDgwa/9dV\n1bMAve+vnT0gyUuAvwQODTiXJKkjC77bJ8kXgNf3uem+Rc7xXuBkVT2TZKG5DgIHAW688cZF3r0k\naakWjH9V3TLXbUm+m+S6qno2yXXAc32G/Qrw60neC7wcuDrJ/1XVz7w+UFVHgaMAo6OjtdidkCQt\nzaDv8z8B3Al8sPf9c7MHVNUfXb6c5C5gtF/4JUmrJ1XLf4Kd5BrgM8CNwP8A76yq7ycZBd5TVe+e\nNf4uLsX/nkXc9xTwnWUvbvGuBb63CvOsRa3ue6v7De3ue0v7/Yaq2rrQoIHivxEkGa+q0WGvYxha\n3fdW9xva3fdW93s+fsJXkhpk/CWpQca/9+6iRrW6763uN7S7763u95yaP+YvSS3ymb8kNai5+Cd5\nZ5Inkvy095bUucbdmmQiyZkkG+JzCYs9C2uSi0m+1vs6sdrr7MpCj2GSlyb5dO/2rybZsfqr7N4i\n9vuuJFMzHuN397uf9SjJx5M8l+Qbc9yeJH/d+7P5epJfWu01rhXNxR/4BvB24CtzDUiyCXgAuA3Y\nDdyRZPfqLG9FLfYsrNNV9Zbe177VW153FvkY3g38oKreBHwE+NDqrrJ7S/i7++kZj/HHVnWRK+sf\ngFvnuf02YGfv6yDwN6uwpjWpufhX1ZNVNbHAsJuBM1X1dFX9BHiIS2cwXe8WdRbWDWIxj+HMP4+H\ngd/JQiegWvs26t/dRamqrwDfn2fIfuATdcmjwJbeqWma01z8F2k78MyM62d729a7Bc/C2vOyJONJ\nHk2yXn9ALOYxvDKmql4AngeuWZXVrZzF/t39/d5hj4eT3LA6S1sTNuq/7SXbkL/Dd74zkVbVz5x/\nqN9d9Nm2Lt4W1cFZWAFurKrJJG8EvpjkdFV9u5sVrprFPIbr9nGex2L26Z+AT1XVj5O8h0v/+/nt\nFV/Z2rARH/Nl2ZDxn+9MpIt0Fpj5bOh6YHLA+1wVHZyFlaqa7H1/OsmXgT3Aeov/Yh7Dy2POJrkK\neBXzHzJYDxbc76r63xlX/44N8FrHEqzbf9td87BPf48BO5PclORq4HYuncF0vbt8FlaY4yysSV6d\n5KW9y9cCvwZ8c9VW2J3FPIYz/zzeAXyx1v8HXxbc71nHuPcBT67i+obtBPCu3rt+3go8f/lQaHOq\nqqkv4Pe49NP/x8B3gbHe9m1c+qUzl8f9LvAtLj3jvW/Y6+5o36/h0rt8nup9f01v+yjwsd7lXwVO\nA//V+373sNc9wP7+zGMI3A/s611+GfBZ4Azwn8Abh73mVdrvDwBP9B7jLwE/N+w1d7jvnwKeBS70\n/p3fDbyHS2cZhkuHfR7o/dmc5tJZhoe+7mF8+QlfSWqQh30kqUHGX5IaZPwlqUHGX5IaZPwlqUHG\nX5IaZPwlqUHGX5Ia9P+8wsHk42/KHgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X[:, 0],X[:, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.1214814 , 0.26641932],\n",
       "       [0.26641932, 0.12395706]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy covariance matrix (normalized by N)\n",
    "np.cov(X.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.48592561, 1.06567728],\n",
       "       [1.06567728, 0.49582826]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# X.T*X (not normalized by N)\n",
    "np.dot(X.T,X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "e, f = np.linalg.eig(np.dot(X.T, X))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "a, b = np.linalg.eig(np.cov(X.T));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.97006433, -0.24284809],\n",
       "       [ 0.24284809,  0.97006433]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# eigenvectors (PCs)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.97006433, -0.24284809],\n",
       "       [ 0.24284809,  0.97006433]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# eigenvectors (PCs)\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.18817741, 0.05726105])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# eigenvalues of cov matrix normed by N\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.2454384654769153"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.75270966, 0.2290442 ])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# eigenvalues of cov matrix NOT normed by N\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.981753861907661"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"70b60e43-1745-414f-869a-8ade3845a9c5\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  var docs_json = {\"8ddf5637-5ca4-45d8-9c97-338a1c109727\":{\"roots\":{\"references\":[{\"attributes\":{\"children\":[{\"id\":\"c4bb55a9-ceab-4198-9c89-d8b67ba4b7e8\",\"type\":\"WidgetBox\"},{\"id\":\"aa515550-12e9-4a26-859f-401e80f01e53\",\"type\":\"WidgetBox\"},{\"id\":\"e6ab6c4d-5af0-43f7-ad56-c6b761162f21\",\"subtype\":\"Figure\",\"type\":\"Plot\"}]},\"id\":\"e9840a79-2c3c-4f28-8fa0-b80b3427f8e6\",\"type\":\"Column\"},{\"attributes\":{\"below\":[{\"id\":\"c4e6d378-a8da-4be2-872e-a41e136b1729\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"a8d4ec35-e609-4ac7-b5de-de44144115c1\",\"type\":\"LinearAxis\"}],\"match_aspect\":true,\"renderers\":[{\"id\":\"c4e6d378-a8da-4be2-872e-a41e136b1729\",\"type\":\"LinearAxis\"},{\"id\":\"89057301-3a94-42d1-99c3-e33e9d4b903e\",\"type\":\"Grid\"},{\"id\":\"a8d4ec35-e609-4ac7-b5de-de44144115c1\",\"type\":\"LinearAxis\"},{\"id\":\"a21ff427-6765-4fc6-ad13-008957479f3c\",\"type\":\"Grid\"},{\"id\":\"fa53379d-89e4-4a48-82a9-f12678fb8cce\",\"type\":\"BoxAnnotation\"},{\"id\":\"fb890285-2583-4924-960b-40f05a22cf44\",\"type\":\"GlyphRenderer\"},{\"id\":\"b0dacfb8-ee76-47dc-a0a9-82999d4a77f4\",\"type\":\"GlyphRenderer\"},{\"id\":\"c7116ddb-2ef5-4e2e-b121-6c262f876bcb\",\"type\":\"GlyphRenderer\"},{\"id\":\"7cc65d9c-9b70-415f-b381-c4f26fe30931\",\"type\":\"GlyphRenderer\"},{\"id\":\"db48cec2-9418-4f95-9ec0-8f88b1cd66d5\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"2eaffe5d-a3e4-4601-a863-3f33a5274f42\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"b5879a31-551c-44c1-b27e-6c618e622384\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"25e1a073-0dba-42f4-8b24-ab126482eeab\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"e3e5872b-6b03-4ba1-b822-aa9ad5e7789e\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"08081845-8ec3-4e89-b1d3-b72a7e1190fa\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"74e08994-d9c9-4640-bd66-282437ab2018\",\"type\":\"LinearScale\"}},\"id\":\"8a62a81a-a593-4f60-9dfe-0964afabbc90\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"below\":[{\"id\":\"4bf345d8-623c-4837-9581-ca9da168ab22\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"2a6aa451-2eb3-479f-a96a-abaedeb207ca\",\"type\":\"LinearAxis\"}],\"plot_height\":300,\"plot_width\":300,\"renderers\":[{\"id\":\"4bf345d8-623c-4837-9581-ca9da168ab22\",\"type\":\"LinearAxis\"},{\"id\":\"6104f9e2-d4ed-49fc-94c7-e82c53ce22ec\",\"type\":\"Grid\"},{\"id\":\"2a6aa451-2eb3-479f-a96a-abaedeb207ca\",\"type\":\"LinearAxis\"},{\"id\":\"7f4828f0-e029-4957-b14f-848edc0c5bd6\",\"type\":\"Grid\"},{\"id\":\"7f98a808-4008-4457-a0fc-ef7765dc54e9\",\"type\":\"BoxAnnotation\"},{\"id\":\"73d54b47-5686-46b4-8271-0a396474bb9a\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"2620a00c-a690-453d-9d84-f5efe14f6c2e\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"2d0d936b-cdfa-4d4c-86e6-c2d6af03f674\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"b6af6263-22e2-4097-93b4-9c12bce4f5d9\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"45955985-fa8f-4251-956e-dca1f0618833\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"76b25579-da86-4520-9917-8e61a3057a76\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"a859ca99-c7f3-4764-8dd8-089e3384dc08\",\"type\":\"LinearScale\"}},\"id\":\"e6ab6c4d-5af0-43f7-ad56-c6b761162f21\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"99396b01-a42f-4925-9b36-cfee197fb83c\",\"type\":\"Selection\"},{\"attributes\":{\"plot\":null,\"text\":\"\"},\"id\":\"2620a00c-a690-453d-9d84-f5efe14f6c2e\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"e22fdab9-78ce-494d-9fbd-7af58c1fd1a3\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"d\"},\"y\":{\"field\":\"dzs\"}},\"id\":\"cb2149c9-3036-49d9-9efa-0ef72cbd8631\",\"type\":\"Circle\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"f0eb05d0-9863-4ea4-aa62-ccdf9c6742d0\",\"type\":\"PanTool\"},{\"id\":\"b147f9be-3b1c-4247-b979-c61d5a65c010\",\"type\":\"WheelZoomTool\"},{\"id\":\"41e393ab-6041-4b99-b63f-60675cbe2993\",\"type\":\"BoxZoomTool\"},{\"id\":\"f57e1ca3-fda8-408a-a158-ac2c6062393e\",\"type\":\"SaveTool\"},{\"id\":\"af4fec0b-d83a-4db3-93d6-3653606f0b34\",\"type\":\"ResetTool\"},{\"id\":\"9356da28-037f-4b5d-a25d-b220df7838b4\",\"type\":\"HelpTool\"}]},\"id\":\"b5879a31-551c-44c1-b27e-6c618e622384\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"13675482-59ec-4708-b94a-7552c1c17413\",\"type\":\"Selection\"},{\"attributes\":{\"callback\":null,\"end\":2.5,\"start\":-2.5},\"id\":\"25e1a073-0dba-42f4-8b24-ab126482eeab\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"a7a90935-c28f-4c0c-8588-e35b52733ebf\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"6bac1760-59d5-4319-8b47-ac4c5287ea7c\",\"type\":\"PanTool\"},{\"id\":\"105e6258-7ead-4e44-be24-b31bd54a9a82\",\"type\":\"WheelZoomTool\"},{\"id\":\"6c623b66-78b7-4809-846f-f6ced7921738\",\"type\":\"BoxZoomTool\"},{\"id\":\"e8c16271-927e-457a-9bf5-fb332f029d59\",\"type\":\"SaveTool\"},{\"id\":\"1d25b24d-b642-47d4-97f8-4767abf9fe9d\",\"type\":\"ResetTool\"},{\"id\":\"9db9cbed-0471-4274-9ff4-a912e6121437\",\"type\":\"HelpTool\"}]},\"id\":\"2d0d936b-cdfa-4d4c-86e6-c2d6af03f674\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"9f0b31cc-6575-412c-9fe7-7152327dadf2\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"callback\":null,\"end\":2.5,\"start\":-2.5},\"id\":\"08081845-8ec3-4e89-b1d3-b72a7e1190fa\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"aa9d287d-9f82-4ff7-97a1-ff157ad6b7b6\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"e3e5872b-6b03-4ba1-b822-aa9ad5e7789e\",\"type\":\"LinearScale\"},{\"attributes\":{\"plot\":{\"id\":\"8a62a81a-a593-4f60-9dfe-0964afabbc90\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"3c585fa9-b3e9-42cc-b85c-057bf4faa2de\",\"type\":\"BasicTicker\"}},\"id\":\"89057301-3a94-42d1-99c3-e33e9d4b903e\",\"type\":\"Grid\"},{\"attributes\":{\"children\":[{\"id\":\"6c197daf-5c47-43a2-9a87-38a02cdf990f\",\"type\":\"Div\"}]},\"id\":\"aa515550-12e9-4a26-859f-401e80f01e53\",\"type\":\"WidgetBox\"},{\"attributes\":{\"formatter\":{\"id\":\"be30ed79-17b8-43ed-a2c6-6eb54d3f29b6\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"8a62a81a-a593-4f60-9dfe-0964afabbc90\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"bd3d2281-0cb0-41e7-97d0-2489745ed4e6\",\"type\":\"BasicTicker\"}},\"id\":\"a8d4ec35-e609-4ac7-b5de-de44144115c1\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"74e08994-d9c9-4640-bd66-282437ab2018\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"bd3d2281-0cb0-41e7-97d0-2489745ed4e6\",\"type\":\"BasicTicker\"},{\"attributes\":{\"formatter\":{\"id\":\"83d80642-8ed0-4a60-9939-ec5a700b7d19\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"8a62a81a-a593-4f60-9dfe-0964afabbc90\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"3c585fa9-b3e9-42cc-b85c-057bf4faa2de\",\"type\":\"BasicTicker\"}},\"id\":\"c4e6d378-a8da-4be2-872e-a41e136b1729\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"3c585fa9-b3e9-42cc-b85c-057bf4faa2de\",\"type\":\"BasicTicker\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"8a62a81a-a593-4f60-9dfe-0964afabbc90\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"bd3d2281-0cb0-41e7-97d0-2489745ed4e6\",\"type\":\"BasicTicker\"}},\"id\":\"a21ff427-6765-4fc6-ad13-008957479f3c\",\"type\":\"Grid\"},{\"attributes\":{\"children\":[{\"id\":\"32a1be05-7744-446f-9f2b-e7f6b745c4d9\",\"type\":\"Row\"}]},\"id\":\"4eabefbf-aff8-4ef7-8d74-5486115a6b9b\",\"type\":\"Column\"},{\"attributes\":{\"callback\":null,\"end\":5,\"start\":-5},\"id\":\"b6af6263-22e2-4097-93b4-9c12bce4f5d9\",\"type\":\"Range1d\"},{\"attributes\":{\"source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"}},\"id\":\"6abc8c9c-de4d-4d4b-934a-7a060fa7f6af\",\"type\":\"CDSView\"},{\"attributes\":{\"args\":{\"ang\":{\"id\":\"e1bfda12-e73f-4e19-96e5-25b95b7b9f8a\",\"type\":\"Slider\"},\"div\":{\"id\":\"6c197daf-5c47-43a2-9a87-38a02cdf990f\",\"type\":\"Div\"},\"source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"}},\"code\":\"\\n    var data = source.data;\\n    var x0 = data['x0']\\n    var x1 = data['x1']\\n    \\n    var d = data['d']\\n        \\n    var z0 = data['z0']\\n    var z1 = data['z1']\\n    \\n    var x0s = data['x0s']\\n    var x1s = data['x1s']\\n    \\n    var A = ang.value*Math.PI/180;\\n\\n    var w = [Math.cos(A), Math.sin(A)];\\n\\n    for (var i = 0; i < x0.length; i++) {\\n        d[i] = x0[i]*w[0] + x1[i]*w[1];\\n        z0[i] = w[0]*d[i];\\n        z1[i] = w[1]*d[i];\\n        x0s[i] = [x0[i], z0[i]];\\n        x1s[i] = [x1[i], z1[i]];\\n    }\\n\\n    data['z0'] = z0\\n    data['z1'] = z1\\n    data['x0s'] = x0s\\n    data['x1s'] = x1s\\n    data['d'] = d\\n    \\n    var V = 0\\n    var line = \\\"\\\"\\n    for (var j = 0; j < d.length; j++) {\\n        V = V + Math.pow(d[j], 2);\\n        line = line + Number(d[j]).toFixed(2) + \\\"\\\\n\\\"\\n    }\\n    \\n    div.text = \\\"Variance along new axis: \\\" + Number(V).toFixed(2) + \\\"\\\\nCoordinates along new axis: \\\\n\\\";\\n    div.text = div.text + line\\n    \\n    source.change.emit();\\n\"},\"id\":\"ec132b59-7df3-4bd6-b36e-e3fa2605266b\",\"type\":\"CustomJS\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"fa53379d-89e4-4a48-82a9-f12678fb8cce\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"f0eb05d0-9863-4ea4-aa62-ccdf9c6742d0\",\"type\":\"PanTool\"},{\"attributes\":{\"callback\":null,\"end\":2.5,\"start\":-2.5},\"id\":\"76b25579-da86-4520-9917-8e61a3057a76\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"b147f9be-3b1c-4247-b979-c61d5a65c010\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"overlay\":{\"id\":\"fa53379d-89e4-4a48-82a9-f12678fb8cce\",\"type\":\"BoxAnnotation\"}},\"id\":\"41e393ab-6041-4b99-b63f-60675cbe2993\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"f57e1ca3-fda8-408a-a158-ac2c6062393e\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"af4fec0b-d83a-4db3-93d6-3653606f0b34\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"9356da28-037f-4b5d-a25d-b220df7838b4\",\"type\":\"HelpTool\"},{\"attributes\":{\"plot\":{\"id\":\"e6ab6c4d-5af0-43f7-ad56-c6b761162f21\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"64bd03b1-661f-4547-933f-a6e90eb676c6\",\"type\":\"BasicTicker\"}},\"id\":\"6104f9e2-d4ed-49fc-94c7-e82c53ce22ec\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"45955985-fa8f-4251-956e-dca1f0618833\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"a859ca99-c7f3-4764-8dd8-089e3384dc08\",\"type\":\"LinearScale\"},{\"attributes\":{\"formatter\":{\"id\":\"aa9d287d-9f82-4ff7-97a1-ff157ad6b7b6\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"e6ab6c4d-5af0-43f7-ad56-c6b761162f21\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"64bd03b1-661f-4547-933f-a6e90eb676c6\",\"type\":\"BasicTicker\"}},\"id\":\"4bf345d8-623c-4837-9581-ca9da168ab22\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"64bd03b1-661f-4547-933f-a6e90eb676c6\",\"type\":\"BasicTicker\"},{\"attributes\":{\"formatter\":{\"id\":\"9f0b31cc-6575-412c-9fe7-7152327dadf2\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"e6ab6c4d-5af0-43f7-ad56-c6b761162f21\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"bd6180e1-1523-41cd-b1cd-631747e04982\",\"type\":\"BasicTicker\"}},\"id\":\"2a6aa451-2eb3-479f-a96a-abaedeb207ca\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"bd6180e1-1523-41cd-b1cd-631747e04982\",\"type\":\"BasicTicker\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"e6ab6c4d-5af0-43f7-ad56-c6b761162f21\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"bd6180e1-1523-41cd-b1cd-631747e04982\",\"type\":\"BasicTicker\"}},\"id\":\"7f4828f0-e029-4957-b14f-848edc0c5bd6\",\"type\":\"Grid\"},{\"attributes\":{\"data_source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"f870a3cf-126b-46c0-a518-5c67a0c73d0b\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"cb2149c9-3036-49d9-9efa-0ef72cbd8631\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"6abc8c9c-de4d-4d4b-934a-7a060fa7f6af\",\"type\":\"CDSView\"}},\"id\":\"73d54b47-5686-46b4-8271-0a396474bb9a\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.6},\"fill_color\":{\"value\":\"firebrick\"},\"line_alpha\":{\"value\":0.6},\"line_color\":{\"value\":\"firebrick\"},\"x\":{\"field\":\"d\"},\"y\":{\"field\":\"dzs\"}},\"id\":\"f870a3cf-126b-46c0-a518-5c67a0c73d0b\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"be30ed79-17b8-43ed-a2c6-6eb54d3f29b6\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"data_source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"636e10a1-7ab9-43f4-a228-4b79cd871989\",\"type\":\"MultiLine\"},\"hover_glyph\":null,\"muted_glyph\":null,\"view\":{\"id\":\"32b48c44-d9fd-481c-be36-25bf73ff1cca\",\"type\":\"CDSView\"}},\"id\":\"db48cec2-9418-4f95-9ec0-8f88b1cd66d5\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"7f98a808-4008-4457-a0fc-ef7765dc54e9\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"6bac1760-59d5-4319-8b47-ac4c5287ea7c\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"105e6258-7ead-4e44-be24-b31bd54a9a82\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"overlay\":{\"id\":\"7f98a808-4008-4457-a0fc-ef7765dc54e9\",\"type\":\"BoxAnnotation\"}},\"id\":\"6c623b66-78b7-4809-846f-f6ced7921738\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"e8c16271-927e-457a-9bf5-fb332f029d59\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1d25b24d-b642-47d4-97f8-4767abf9fe9d\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"9db9cbed-0471-4274-9ff4-a912e6121437\",\"type\":\"HelpTool\"},{\"attributes\":{\"data_source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"481ee3a5-2738-4186-9c76-18cef3fef785\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"0fbcbcac-5e44-4f8a-8eb8-680190af0db6\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"a71a4fa2-1192-41cd-9785-adb0dd9f47eb\",\"type\":\"CDSView\"}},\"id\":\"fb890285-2583-4924-960b-40f05a22cf44\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.6},\"fill_color\":{\"value\":\"navy\"},\"line_alpha\":{\"value\":0.6},\"line_color\":{\"value\":\"navy\"},\"size\":{\"units\":\"screen\",\"value\":5},\"x\":{\"field\":\"x0\"},\"y\":{\"field\":\"x1\"}},\"id\":\"481ee3a5-2738-4186-9c76-18cef3fef785\",\"type\":\"Circle\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"size\":{\"units\":\"screen\",\"value\":5},\"x\":{\"field\":\"x0\"},\"y\":{\"field\":\"x1\"}},\"id\":\"0fbcbcac-5e44-4f8a-8eb8-680190af0db6\",\"type\":\"Circle\"},{\"attributes\":{\"source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"}},\"id\":\"a71a4fa2-1192-41cd-9785-adb0dd9f47eb\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"83d80642-8ed0-4a60-9939-ec5a700b7d19\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"line_alpha\":0.5,\"x\":{\"field\":\"z0\"},\"y\":{\"field\":\"z1\"}},\"id\":\"73e95a1b-e827-4b50-b31c-6c96f6be8923\",\"type\":\"Line\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"#1f77b4\",\"x\":{\"field\":\"z0\"},\"y\":{\"field\":\"z1\"}},\"id\":\"3da7cd9a-ac60-41eb-80da-4db8eaa2a564\",\"type\":\"Line\"},{\"attributes\":{\"data_source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"73e95a1b-e827-4b50-b31c-6c96f6be8923\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"3da7cd9a-ac60-41eb-80da-4db8eaa2a564\",\"type\":\"Line\"},\"selection_glyph\":null,\"view\":{\"id\":\"2183ef00-0afd-4c5f-84c8-5cd727ffe9fc\",\"type\":\"CDSView\"}},\"id\":\"b0dacfb8-ee76-47dc-a0a9-82999d4a77f4\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"}},\"id\":\"2183ef00-0afd-4c5f-84c8-5cd727ffe9fc\",\"type\":\"CDSView\"},{\"attributes\":{\"plot\":null,\"text\":\"\"},\"id\":\"2eaffe5d-a3e4-4601-a863-3f33a5274f42\",\"type\":\"Title\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.6},\"fill_color\":{\"value\":\"firebrick\"},\"line_alpha\":{\"value\":0.6},\"line_color\":{\"value\":\"firebrick\"},\"x\":{\"field\":\"z0\"},\"y\":{\"field\":\"z1\"}},\"id\":\"4fec3375-48df-4214-8a52-812eac77d0e4\",\"type\":\"Circle\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"z0\"},\"y\":{\"field\":\"z1\"}},\"id\":\"54e3fa90-e59d-405c-b2cd-cd7331310b08\",\"type\":\"Circle\"},{\"attributes\":{\"data_source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"4fec3375-48df-4214-8a52-812eac77d0e4\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"54e3fa90-e59d-405c-b2cd-cd7331310b08\",\"type\":\"Circle\"},\"selection_glyph\":null,\"view\":{\"id\":\"57988135-3c46-4cd2-86be-84e5c1385f7c\",\"type\":\"CDSView\"}},\"id\":\"c7116ddb-2ef5-4e2e-b121-6c262f876bcb\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"}},\"id\":\"57988135-3c46-4cd2-86be-84e5c1385f7c\",\"type\":\"CDSView\"},{\"attributes\":{\"line_color\":{\"value\":\"#8073ac\"},\"xs\":{\"field\":\"x0s\"},\"ys\":{\"field\":\"x1s\"}},\"id\":\"636e10a1-7ab9-43f4-a228-4b79cd871989\",\"type\":\"MultiLine\"},{\"attributes\":{\"line_alpha\":0.2,\"line_color\":\"#1f77b4\",\"line_width\":0.5,\"x\":{\"field\":\"e0\"},\"y\":{\"field\":\"e1\"}},\"id\":\"612d9128-60c5-469a-9acf-5e1cb62bbe7c\",\"type\":\"Line\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"#1f77b4\",\"line_width\":0.5,\"x\":{\"field\":\"e0\"},\"y\":{\"field\":\"e1\"}},\"id\":\"c01b03f6-3066-4acd-9777-bfd5f1fa488d\",\"type\":\"Line\"},{\"attributes\":{\"data_source\":{\"id\":\"2dae920d-ba8c-421a-8e37-b900512ddb04\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"612d9128-60c5-469a-9acf-5e1cb62bbe7c\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"c01b03f6-3066-4acd-9777-bfd5f1fa488d\",\"type\":\"Line\"},\"selection_glyph\":null,\"view\":{\"id\":\"4b5b514d-5c92-42ca-a1b6-cbc5b7a20d4c\",\"type\":\"CDSView\"}},\"id\":\"7cc65d9c-9b70-415f-b381-c4f26fe30931\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"source\":{\"id\":\"2dae920d-ba8c-421a-8e37-b900512ddb04\",\"type\":\"ColumnDataSource\"}},\"id\":\"4b5b514d-5c92-42ca-a1b6-cbc5b7a20d4c\",\"type\":\"CDSView\"},{\"attributes\":{\"source\":{\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"}},\"id\":\"32b48c44-d9fd-481c-be36-25bf73ff1cca\",\"type\":\"CDSView\"},{\"attributes\":{\"height\":100,\"text\":\"Variance along new axis: \",\"width\":200},\"id\":\"6c197daf-5c47-43a2-9a87-38a02cdf990f\",\"type\":\"Div\"},{\"attributes\":{\"children\":[{\"id\":\"8a62a81a-a593-4f60-9dfe-0964afabbc90\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"id\":\"e9840a79-2c3c-4f28-8fa0-b80b3427f8e6\",\"type\":\"Column\"}]},\"id\":\"32a1be05-7744-446f-9f2b-e7f6b745c4d9\",\"type\":\"Row\"},{\"attributes\":{\"children\":[{\"id\":\"e1bfda12-e73f-4e19-96e5-25b95b7b9f8a\",\"type\":\"Slider\"}]},\"id\":\"c4bb55a9-ceab-4198-9c89-d8b67ba4b7e8\",\"type\":\"WidgetBox\"},{\"attributes\":{\"callback\":{\"id\":\"ec132b59-7df3-4bd6-b36e-e3fa2605266b\",\"type\":\"CustomJS\"},\"end\":360,\"start\":0,\"step\":0.1,\"title\":\"Angle\",\"value\":1},\"id\":\"e1bfda12-e73f-4e19-96e5-25b95b7b9f8a\",\"type\":\"Slider\"},{\"attributes\":{\"callback\":null,\"data\":{\"e0\":{\"__ndarray__\":\"y5sNuLpmI8DLmw24umYjQA==\",\"dtype\":\"float64\",\"shape\":[2]},\"e1\":{\"__ndarray__\":\"CmPVZYdtA8AKY9Vlh20DQA==\",\"dtype\":\"float64\",\"shape\":[2]}},\"selected\":{\"id\":\"13675482-59ec-4708-b94a-7552c1c17413\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"a7a90935-c28f-4c0c-8588-e35b52733ebf\",\"type\":\"UnionRenderers\"}},\"id\":\"2dae920d-ba8c-421a-8e37-b900512ddb04\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"callback\":null,\"data\":{\"d\":{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fwAAAAAAAPh/AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[5]},\"dzs\":{\"__ndarray__\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\",\"dtype\":\"float64\",\"shape\":[5]},\"x0\":{\"__ndarray__\":\"4IGmLehF0r/4nqz2a5frP6TkiQ26LvG/nAj/oUhy9T8ApsMIlfvqvw==\",\"dtype\":\"float64\",\"shape\":[5]},\"x0s\":[{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]},{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]},{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]},{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]},{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]}],\"x1\":{\"__ndarray__\":\"yZb4Pc/D07+v3ewmRingP3sE4otUxda/cUfosotDwT/IwssoGk2ZPw==\",\"dtype\":\"float64\",\"shape\":[5]},\"x1s\":[{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]},{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]},{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]},{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]},{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[2]}],\"z0\":{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fwAAAAAAAPh/AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[5]},\"z1\":{\"__ndarray__\":\"AAAAAAAA+H8AAAAAAAD4fwAAAAAAAPh/AAAAAAAA+H8AAAAAAAD4fw==\",\"dtype\":\"float64\",\"shape\":[5]}},\"selected\":{\"id\":\"99396b01-a42f-4925-9b36-cfee197fb83c\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"e22fdab9-78ce-494d-9fbd-7af58c1fd1a3\",\"type\":\"UnionRenderers\"}},\"id\":\"bc4756ad-ec3d-4f79-b029-a2d4ee136cb2\",\"type\":\"ColumnDataSource\"}],\"root_ids\":[\"4eabefbf-aff8-4ef7-8d74-5486115a6b9b\"]},\"title\":\"Bokeh Application\",\"version\":\"0.13.0\"}};\n",
       "  var render_items = [{\"docid\":\"8ddf5637-5ca4-45d8-9c97-338a1c109727\",\"roots\":{\"4eabefbf-aff8-4ef7-8d74-5486115a6b9b\":\"70b60e43-1745-414f-869a-8ade3845a9c5\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        embed_document(root);\n",
       "        clearInterval(timer);\n",
       "      }\n",
       "      attempts++;\n",
       "      if (attempts > 100) {\n",
       "        console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\")\n",
       "        clearInterval(timer);\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "4eabefbf-aff8-4ef7-8d74-5486115a6b9b"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# bokeh plot\n",
    "\n",
    "# pre-populate data source object\n",
    "z0 = np.nan*np.zeros(len(X))\n",
    "z1 = np.nan*np.zeros(len(X))\n",
    "x0s = [np.array(np.nan*np.zeros(2)) for i in range(0,len(X))]\n",
    "x1s = [np.array(np.nan*np.zeros(2)) for i in range(0,len(X))]\n",
    "d = np.nan*np.zeros(len(X))\n",
    "dzs = np.zeros(len(X))\n",
    "\n",
    "# data source\n",
    "source_data = ColumnDataSource(data=dict(x0=X[:,0], x1=X[:,1], z0=z0, z1=z1, x0s=x0s, x1s=x1s, d=d, dzs=dzs))\n",
    "\n",
    "# data source for eigenvector line\n",
    "eig_source = ColumnDataSource(data=dict(e0=np.array([-10*b[0,0], 10*b[0,0]]), e1=np.array([-10*b[1,0], 10*b[1,0]])))\n",
    "\n",
    "# main plot\n",
    "plot = figure(x_range=(-2.5, 2.5), y_range=(-2.5, 2.5), plot_width=600, plot_height=600, match_aspect=True)\n",
    "\n",
    "# plot the new components in the mini-plot on the right\n",
    "var_plot = figure(x_range=(-5, 5), y_range=(-2.5, 2.5), plot_width = 300, plot_height = 300)\n",
    "var_plot.scatter('d', 'dzs', source=source_data, color='firebrick', alpha=0.6)\n",
    "\n",
    "# plot the data\n",
    "plot.scatter('x0', 'x1', source=source_data, color='navy', alpha=0.6, size=5)\n",
    "\n",
    "# plot the new axis\n",
    "plot.line('z0', 'z1', source=source_data, color='black', alpha=0.5)\n",
    "\n",
    "# plot the projections of the data onto the rotating line\n",
    "plot.scatter('z0', 'z1', source=source_data, color='firebrick', alpha=0.6)\n",
    "\n",
    "# plot the line indicating the direction of the first eigenvector\n",
    "plot.line('e0','e1', source=eig_source, line_width=0.5, line_alpha=0.2)\n",
    "\n",
    "# plot all the lines connecting the data dots to the projection dots\n",
    "glyph = MultiLine(xs = \"x0s\", ys = \"x1s\", line_color=\"#8073ac\", line_width=1)\n",
    "plot.add_glyph(source_data, glyph)\n",
    "\n",
    "# add output text\n",
    "div = Div(text='Variance along new axis: ', width=200, height=100)\n",
    "\n",
    "# the javascript code that calculates all the projection dots depending upon the user-chosen angle for the line to project onto\n",
    "callback = CustomJS(args=dict(source=source_data, div=div), code=\"\"\"\n",
    "    var data = source.data;\n",
    "    var x0 = data['x0']\n",
    "    var x1 = data['x1']\n",
    "    \n",
    "    var d = data['d']\n",
    "        \n",
    "    var z0 = data['z0']\n",
    "    var z1 = data['z1']\n",
    "    \n",
    "    var x0s = data['x0s']\n",
    "    var x1s = data['x1s']\n",
    "    \n",
    "    var A = ang.value*Math.PI/180;\n",
    "\n",
    "    var w = [Math.cos(A), Math.sin(A)];\n",
    "\n",
    "    for (var i = 0; i < x0.length; i++) {\n",
    "        d[i] = x0[i]*w[0] + x1[i]*w[1];\n",
    "        z0[i] = w[0]*d[i];\n",
    "        z1[i] = w[1]*d[i];\n",
    "        x0s[i] = [x0[i], z0[i]];\n",
    "        x1s[i] = [x1[i], z1[i]];\n",
    "    }\n",
    "\n",
    "    data['z0'] = z0\n",
    "    data['z1'] = z1\n",
    "    data['x0s'] = x0s\n",
    "    data['x1s'] = x1s\n",
    "    data['d'] = d\n",
    "    \n",
    "    var V = 0\n",
    "    var line = \"\"\n",
    "    for (var j = 0; j < d.length; j++) {\n",
    "        V = V + Math.pow(d[j], 2);\n",
    "        line = line + Number(d[j]).toFixed(2) + \"\\\\n\"\n",
    "    }\n",
    "    \n",
    "    div.text = \"Variance along new axis: \" + Number(V).toFixed(2) + \"\\\\nCoordinates along new axis: \\\\n\";\n",
    "    div.text = div.text + line\n",
    "    \n",
    "    source.change.emit();\n",
    "\"\"\")\n",
    "\n",
    "# make the slider bar\n",
    "ang_slider = Slider(start=0, end=360, value=1, step=.1, title=\"Angle\", callback=callback)\n",
    "callback.args[\"ang\"] = ang_slider\n",
    "\n",
    "# plot layout\n",
    "lo = layout([\n",
    "  [plot, [widgetbox(ang_slider), widgetbox(div), var_plot]],\n",
    "])\n",
    "\n",
    "# write\n",
    "output_file(\"pca.html\", title=\"pca.py example\")\n",
    "\n",
    "# display\n",
    "show(lo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[The inspiration](https://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.97006433  0.24284809]\n",
      " [-0.24284809  0.97006433]]\n",
      "[1.18817741 0.05726105]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "pca = PCA(n_components=2)\n",
    "pca.fit(X)\n",
    "print(pca.components_)  \n",
    "print(pca.explained_variance_)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.2454384654769155"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(pca.explained_variance_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "299.4207085516162"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.explained_variance_[0]*252"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.97006433, -0.24284809],\n",
       "       [ 0.24284809,  0.97006433]])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P = pca.components_.T\n",
    "P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.28551678, -0.30882627],\n",
       "       [ 0.8622341 ,  0.50503833],\n",
       "       [-1.0739079 , -0.35579408],\n",
       "       [ 1.3404013 ,  0.13487383],\n",
       "       [-0.84321071,  0.02470818]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 2)"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Y = np.dot(P.T, X.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.35196751,  0.95907014, -1.12816366,  1.33302934, -0.8119683 ])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.35196751,  0.95907014, -1.12816366,  1.33302934, -0.8119683 ],\n",
       "       [-0.23024414,  0.28052777, -0.08434667, -0.1946776 ,  0.22874064]])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "num_data = 10\n",
    "Y = norm.rvs(size=(num_data,3), random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "\n",
    "class RiskModelPCA():\n",
    "    \n",
    "    ANN_FACTOR = 252\n",
    "    \n",
    "    def __init__(self, num_factors):\n",
    "        self._num_factors = num_factors\n",
    "        self.num_stocks_ = None\n",
    "        self.factor_betas_ = None\n",
    "        self.factor_returns_ = None\n",
    "        self.common_returns_ = None\n",
    "        self.residuals_ = None\n",
    "        self.factor_cov_matrix_ = None\n",
    "        self.idio_var_matrix_ = None\n",
    "        self.explained_variance_ratio_ = None\n",
    "\n",
    "    def fit(self, returns):\n",
    "        self.num_stocks_ = len(returns.columns)\n",
    "        mod = PCA(n_components=self._num_factors, svd_solver='full')\n",
    "        mod.fit(returns)\n",
    "        \n",
    "        self.factor_betas_ = pd.DataFrame(\n",
    "            data=mod.components_.T,\n",
    "            index=returns.columns\n",
    "        )\n",
    "        \n",
    "        self.factor_returns_ = pd.DataFrame(\n",
    "            data=mod.transform(rets),\n",
    "            index=returns.index\n",
    "        )\n",
    "        \n",
    "        self.explained_variance_ratio_ = mod.explained_variance_ratio_\n",
    "        \n",
    "        self.common_returns_ = pd.DataFrame(\n",
    "            data=np.dot(self.factor_returns_, self.factor_betas_.T),\n",
    "            index=returns.index\n",
    "        )\n",
    "        self.common_returns_.columns = returns.columns\n",
    "        \n",
    "        self.residuals_ = (returns - self.common_returns_)\n",
    "        \n",
    "        self.factor_cov_matrix_ = np.diag(\n",
    "            self.factor_returns_.var(axis=0, ddof=1)*RiskModelPCA.ANN_FACTOR\n",
    "        )\n",
    "        \n",
    "        self.idio_var_matrix_ = pd.DataFrame(\n",
    "            data=np.diag(np.var(self.residuals_))*RiskModelPCA.ANN_FACTOR,\n",
    "            index=returns.columns\n",
    "        )\n",
    "        \n",
    "        self.idio_var_vector_ = pd.DataFrame(\n",
    "            data=np.diag(self.idio_var_matrix_.values),\n",
    "            index=returns.columns\n",
    "        )\n",
    "        \n",
    "        self.idio_var_matrix_.columns = index=returns.columns\n",
    "\n",
    "    def get_factor_exposures(self, weights):\n",
    "        F = self.factor_betas_.loc[weights.index]\n",
    "        return F.T.dot(weights)\n",
    "\n",
    "    def predict(self, weights):\n",
    "        \"\"\" Calculates expected portfolio risk as sqrt(h'XFX'h + h'Sh).\n",
    "            This will fail if your portfolio has asset weights not in the risk model\"\"\"\n",
    "        all_assets = pd.DataFrame(\n",
    "            data=np.repeat(0, self.num_stocks_),\n",
    "            index=self.factor_betas_.index)\n",
    "        all_assets.loc[weights.index] = weights\n",
    "        \n",
    "            \n",
    "        h = all_assets\n",
    "        X = self.factor_betas_\n",
    "        F = self.factor_cov_matrix_\n",
    "        S = self.idio_var_matrix_\n",
    "        \n",
    "        return np.sqrt(h.T.dot(X).dot(F).dot(X.T).dot(h) + h.T.dot(S).dot(h))[0].values[0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAPL</th>\n",
       "      <th>GOOG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.285517</td>\n",
       "      <td>-0.308826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.862234</td>\n",
       "      <td>0.505038</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.073908</td>\n",
       "      <td>-0.355794</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.340401</td>\n",
       "      <td>0.134874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.843211</td>\n",
       "      <td>0.024708</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       AAPL      GOOG\n",
       "0 -0.285517 -0.308826\n",
       "1  0.862234  0.505038\n",
       "2 -1.073908 -0.355794\n",
       "3  1.340401  0.134874\n",
       "4 -0.843211  0.024708"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rets = pd.DataFrame(X)\n",
    "rets.columns = ['AAPL','GOOG']\n",
    "rets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rm = RiskModelPCA(2)\n",
    "rm.fit(rets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AAPL</th>\n",
       "      <td>0.970064</td>\n",
       "      <td>-0.242848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GOOG</th>\n",
       "      <td>0.242848</td>\n",
       "      <td>0.970064</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0         1\n",
       "AAPL  0.970064 -0.242848\n",
       "GOOG  0.242848  0.970064"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# factor betas are the PCs to keep\n",
    "rm.factor_betas_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.28878328e-16, -1.07698775e-16, -4.30795102e-16,\n",
       "         0.00000000e+00, -4.34165296e-16],\n",
       "       [ 7.37206160e-20,  2.69615542e-17,  1.07846217e-16,\n",
       "         0.00000000e+00,  9.43838698e-17]])"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# factor betas are orthogonal to idiosyncratic variance vecs\n",
    "np.dot(rm.factor_betas_.T, rm.residuals_.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.351968</td>\n",
       "      <td>-0.230244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.959070</td>\n",
       "      <td>0.280528</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.128164</td>\n",
       "      <td>-0.084347</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.333029</td>\n",
       "      <td>-0.194678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.811968</td>\n",
       "      <td>0.228741</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1\n",
       "0 -0.351968 -0.230244\n",
       "1  0.959070  0.280528\n",
       "2 -1.128164 -0.084347\n",
       "3  1.333029 -0.194678\n",
       "4 -0.811968  0.228741"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# factor returns are the transformed data—the returns in the new basis, condensed\n",
    "rm.factor_returns_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.75270966e+00, 1.99212200e-16],\n",
       "       [1.99212200e-16, 2.29044202e-01]])"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(rm.factor_returns_.T, rm.factor_returns_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.95402338])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rm.explained_variance_ratio_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAPL</th>\n",
       "      <th>GOOG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.341431</td>\n",
       "      <td>-0.085475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.930360</td>\n",
       "      <td>0.232908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.094391</td>\n",
       "      <td>-0.273972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.293124</td>\n",
       "      <td>0.323724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.787661</td>\n",
       "      <td>-0.197185</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       AAPL      GOOG\n",
       "0 -0.341431 -0.085475\n",
       "1  0.930360  0.232908\n",
       "2 -1.094391 -0.273972\n",
       "3  1.293124  0.323724\n",
       "4 -0.787661 -0.197185"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dimensionally-reduced returns in feature space\n",
    "rm.common_returns_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAPL</th>\n",
       "      <th>GOOG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.055914</td>\n",
       "      <td>-0.223352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.068126</td>\n",
       "      <td>0.272130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.020483</td>\n",
       "      <td>-0.081822</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.047277</td>\n",
       "      <td>-0.188850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.055549</td>\n",
       "      <td>0.221893</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       AAPL      GOOG\n",
       "0  0.055914 -0.223352\n",
       "1 -0.068126  0.272130\n",
       "2  0.020483 -0.081822\n",
       "3  0.047277 -0.188850\n",
       "4 -0.055549  0.221893"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#\n",
    "rm.residuals_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.00270159, -0.01079157],\n",
       "       [-0.01079157,  0.04310726]])"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1/5)*np.dot(rm.residuals_.T, rm.residuals_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[299.42070855]])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# eigenvalue 1 * 252\n",
    "rm.factor_cov_matrix_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAPL</th>\n",
       "      <th>GOOG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AAPL</th>\n",
       "      <td>0.680799</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GOOG</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>10.863028</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          AAPL       GOOG\n",
       "AAPL  0.680799   0.000000\n",
       "GOOG  0.000000  10.863028"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#\n",
    "rm.idio_var_matrix_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AAPL    0.002702\n",
       "GOOG    0.043107\n",
       "dtype: float64"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(rm.residuals_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
